//
// Created by lwj66 on 24-6-7.
//
#include "phoneList.h"

void menu1()
{
    printf("------------------------------------------------\n");
    printf("------------    1. add     2. del       -----------\n");
    printf("------------    3. find    4. modify -----------\n");
    printf("------------    5. show   6. move    -----------\n");
    printf("------------    0. exit    -----------------------\n");
    printf("------------------------------------------------\n\n");
}

void menu2()
{
    printf("------------------------------------------------\n");
    printf("------  1. name  2. tel_num  3. gender  -----\n");
    printf("-----------   4. address  5. exit  ------------\n");
    printf("------------------------------------------------\n\n");
}

void pick(SL* s)
{
    int choice = 1;
    while (choice)
    {
        menu1();
        printf("请输入您的选择 : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case ADD :
                infoAdd(s);
                break;
            case DEL :
                infoDel(s);
                break;
            case FIND :
                infoFind(s);
                break;
            case MODIFY :
                infoModify(s);
                break;
            case SHOW :
                infoShow(s);
                break;
            case MOVE:
                infoMove(s);
                break;
            case EXIT:
                printf("感谢您的使用, 再见!");
                break;
            default:
                printf("输入错误, 请重新输入!\n");
        }
    }
}

void infoInit(SL* ps)
{
    assert(ps);
    ps->info = NULL;
    FILE * file = fopen("contact.txt", "rb");
    if (file == NULL)
    {
        Info* n = (Info*) malloc(INIT_COUNT * sizeof (Info));
        if (n == NULL)
        {
            perror("分配内存失败!");
            exit(1);
        }
        ps->info = n;
        ps->capacity = INIT_COUNT;
        ps->size = 0;
    }
    else
    {
        fseek(file, 0, SEEK_END);
        int filesize = ftell(file);
        rewind(file);
        if (filesize > 4)
        {
            ps->info = (Info*) malloc(filesize - sizeof (int));
            if (fread(&ps->size, sizeof (int), 1, file) +
                        fread(ps->info, filesize - sizeof (int), ps->size,  file) < 2)
            {
                perror("文件读取失败!");
                exit(1);
            }
        }
        ps->capacity = 2 * ps->size;
        fclose(file);
        file = NULL;
    }
}

void infoGrow(SL* s)
{
    assert(s);
    if (s->size != s->capacity)
        return;
    int newCapacity = s->capacity == 0 ? 4 : 2 * s->capacity;
    Info* n = (Info*) realloc(s->info, newCapacity * sizeof (Info));
    if (n == NULL)
    {
        perror("内存重新分配失败!");
        exit(1);
    }
    s->capacity = newCapacity;
    s->info = n;
}
void infoAdd(SL* s)
{
    assert(s);
    infoGrow(s);
    printf("请输入添加联系人的姓名 : ");
    scanf("%s", s->info[s->size].name);
    printf("电话号码 : ");
    scanf("%s", s->info[s->size].tel_num);
    printf("性别 : ");
    scanf("%s", s->info[s->size].gender);
    printf("地址 : ");
    scanf("%s", s->info[s->size].address);
    s->size++;
}

void infoDel(SL* s)
{
    assert(s);
    int index;
    printf("请输入您想要删除的联系人索引 : ");
    int flag = 1;
    while (flag)
    {
        scanf("%d", &index);
        if (index < 0 || index >= s->size)
            printf("输入错误, 请重新输入: ");
        else
        {
            flag = 0;
            for (int i = index; i < s->size - 1; ++i)
                s->info[i] = s->info[i + 1];
            s->size--;
        }
    }
}

void infoFind(SL* s)
{
    assert(s);
    int index;
    printf("请输入您想要查找的联系人索引 : ");
    int flag = 1;
    while (flag)
    {
        scanf("%d", &index);
        if (index < 0 || index >= s->size)
            printf("输入错误, 请重新输入: ");
        else
        {
            flag = 0;
            printf("查找联系人姓名 : %s\n, 电话号码 : %s, \n性别 : %s\n, 地址 : %s.\n",
                   s->info[index].name, s->info[index].tel_num, s->info[index].gender, s->info[index].address);
        }
    }
}

void infoModify(SL* s)
{
    assert(s);
    int index;
    printf("请输入您想要修改的联系人索引 : ");
    int flag1 = 1;
    while (flag1)
    {
        scanf("%d", &index);
        if (index < 0 || index >= s->size)
            printf("输入错误, 请重新输入: ");
        else
        {
            flag1 = 0;
            int flag2 = 1;
            while (flag2)
            {
                menu2();
                printf("请输入您要修改的选项 : \n");
                int choice;
                scanf("%d", &choice);
                switch (choice)
                {
                    case 1:
                        printf("请输入修改后的姓名 : ");
                        scanf("%s", s->info[index].name);
                        break;
                    case 2:
                        printf("请输入修改后的电话号码 : ");
                        scanf("%s", s->info[index].tel_num);
                        break;
                    case 3:
                        printf("请输入修改后的性别 : ");
                        scanf("%s", s->info[index].gender);
                        break;
                    case 4:
                        printf("请输入修改后的地址 : ");
                        scanf("%s", s->info[index].address);
                        break;
                    case 0:
                        flag2 = 0;
                        printf("修改完毕!\n");
                        break;
                    default:
                        printf("输入错误, 请重新输入!\n");
                        break;
                }
            }
        }
    }
}

void infoShow(SL* s)
{
    assert(s);
    printf("------------------------通讯录------------------------\n");
    printf("%-15s%-15s%-10s%s\n", "姓名", "电话号码", "性别", "地址");
    for (int i = 0; i < s->size; ++i)
    {
        printf("%-15s%-20s%-10s%s\n",
               s->info[i].name, s->info[i].tel_num, s->info[i].gender, s->info[i].address);
    }
    printf("--------------------------------------------------------\n");
}

void infoMove(SL* s)
{
    assert(s);
    printf("请输入您想要交换的两个联系人索引 : ");
    int flag = 1, index1, index2;
    Info temp;
    while (flag)
    {
        scanf("%d%d", &index1, &index2);
        if (index1 < 0 || index1 >= s->size || index2 < 0 || index2 >= s->size)
            printf("输入错误, 请重新输入 : ");
        else
        {
            flag = 0;
            temp = s->info[index1];
            s->info[index1] = s->info[index2];
            s->info[index2] = temp;
        }
    }
}

void infoClear(SL* s)
{
    assert(s);
    FILE * file = fopen("contact.txt", "wb");
    if (file == NULL)
    {
        perror("文件打开失败!");
        exit(1);
    }
    if (fwrite(&s->size, sizeof (int), 1, file) +
                fwrite(s->info, sizeof (Info), s->size, file) < 2)
    {
        perror("文件保存失败!");
        exit(1);
    }
    fclose(file);
    file = NULL;
    free(s->info);
    s->info = NULL;
    s->size = s->capacity = 0;
}
